package com.kawins.base.baseabs;

import com.kawins.base.BaseService;
import com.kawins.spring.StringEscapeEditor;
import com.kawins.supers.table.core.TableAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author rankai
 *         createTime 2017-04-2017/4/27 18:58
 */
public abstract class BaseController<T extends BaseModel> extends TableAdapter {

    @Autowired
    protected BaseService<T> adaptService;

    @Override
    protected Logger buildLogger() {
        return LoggerFactory.getLogger(this.getClass());
    }

    @Override
    protected BaseService buildService() {
        return adaptService;
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringEscapeEditor(false, false, false));
    }
}
